<?php

function user_collectbooks($uid) {
	$output = '';

	$output .='<div class="items-list">';

	$output .='<div class="items-list-title">我的收藏</div>';

	$html = drupal_get_form('user_collectlist_form');

	$output .= drupal_render($html);

	$output .= '</div>';

	return $output;

}

function user_collectlist_form() {
	global $user;
	$form = array();

	$query = db_select('user_collect','a')
			->fields('a')
			->condition('a.uid', $user->uid)
			->orderBy('a.created', 'DESC');
  	$items = $query->execute()->fetchAll();

  	$header = array(
  		'id' => t('序号'),
    	'name' => t('书名'),
    	'time' => t('收藏时间'),
  	);
	
	$row = array();
	
	$i = 1;
	foreach($items as $item){
		$node = node_load($item->nid);
		$row[$item->id] = array(
			'id' => $i,
    		'name' => '《'.$node->title.'》', // 'first_name' was the key used in the header
    		'time' => format_date($item->created, 'custom', 'Y-m-d'), // 'last_Name' was the key used in the header
  		);
    	$i++;
	}

	$form['table'] = array(
    	'#type' => 'tableselect',
    	'#header' => $header,
    	'#options' => $row,
    	'#empty' => t('没有收藏的图书！'),
    	'#multiple' => true,
    	'#attributes' => array('class' => array('selectbooks')),
  	);
	
  	$form['submit'] = array( 
		'#type' => 'submit', 
		'#value' => t('删除'),
		'#submit' => array('collectbooks_delete')
  	);
	
	return $form;
}

function collectbooks_delete($form, &$form_state) {
	$ids = array_filter($form_state['values']['table']);
	foreach($ids as $value)
	{
	  	db_delete('user_collect')
	  		->condition('id', $value)
  			->execute();
	}
}

function user_scribe($uid) {
	global $user;
	$output = '';
	$output .= '<div id="dlg-bookdelete"></div>';
	$output .= '<div class="items-list">';

	$output .='<div class="items-list-title">我的借书架</div>';

	$query = db_select('user_scribe','a')
		->fields('a')
		->condition('a.uid', $user->uid)
		->condition('a.status', 2)
		->orderBy('a.created', 'DESC');
  	$items = $query->execute()->fetchAll();

  	$header = array(
  		array('data' => t('序号')),
    	array('data' => t('书名')),
    	array('data' => t('订阅时间'))
  	);
	
	$row = array();
	
	$i = 1;
	foreach($items as $item){
		$node = node_load($item->nid);
		$row[] = array('data' => 
					array(
						$i,
    					'《'.$node->title.'》',
    					format_date($item->created, 'custom', 'Y-m-d')
    				)
  				);
    	$i++;
	}
	

	$output .= theme('table', 
					array(
						'header' =>$header, 
						'rows' => $row, 
						'attributes' => array('class' => array('selectbooks')),
						'caption' => t('我正在阅读的图书')
					)
				);
	
	$query = db_select('user_scribe','a')
		->fields('a')
		->condition('a.uid', $user->uid)
		->condition('a.status', 1)
		->orderBy('a.created', 'DESC');
  	$items = $query->execute()->fetchAll();

  	$header = array(
  		array('data' => t('序号')),
    	array('data' => t('书名')),
    	array('data' => t('订阅时间')),
    	array('data' => t('删除'))
  	);
	
	$row = array();
	
	$i = 1;
	foreach($items as $item){
		$node = node_load($item->nid);
		$row[] = array('data' => 
					array(
						$i,
    					'《'.$node->title.'》',
    					format_date($item->created, 'custom', 'Y-m-d'),
    					'<input class="book-delete book-button" type="submit" value="删除" id="'.$item->id.'">'
    				)
  				);
    	$i++;
	}
	

	$output .= theme('table', 
					array(
						'header' =>$header, 
						'rows' => $row, 
						'attributes' => array('class' => array('selectbooks')),
						'caption' => t('我借阅的图书')
					)
				);
	$output .= '</div>';

	$form = drupal_get_form('select_book_form');

	$output .= drupal_render($form);
	
	return $output;

}

function select_book_form() {
	global $user;

	$form = array();

	$query = db_select('user_scribe','a')
		->fields('a')
		->condition('a.uid', $user->uid)
		->condition('a.status', 1)
		->orderBy('a.created', 'DESC');

  	$num = $query->execute()->rowCount();
  	
	if($num == 5) {
		$form['submit'] = array(
			'#type' => 'submit',
			'#value' => t('下一步'),
			
		);
	}else{
		$form['submit'] = array(
			'#type' => 'submit',
			'#value' => t('下一步'),
			'#disabled' => true 
		);
	}

	
	return $form;
}

function select_book_form_submit(&$form, &$form_state) {

	$form_state['redirect'] = 'step_1';
	
}